﻿using System;
using System.Data;
using System.Data.Common;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Admin_User_Add : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			this.Form.Action = "Backend/User/Add.aspx";

			// Bind all available roles
			((DropDownList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("UserRole")).DataSource = Roles.GetAllRoles();
			((DropDownList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("UserRole")).DataBind();
			((DropDownList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("UserRole")).Items[0].Selected = true;
		}
	}

	protected void efCreateUser_CreatedUser(object sender, EventArgs e)
	{
		ProfileCommon userProfile = (ProfileCommon)ProfileCommon.Create(CreateUser.UserName, true);

		// Update the profile values
		userProfile.FirstName = ((TextBox)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;
		userProfile.LastName = ((TextBox)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;
		userProfile.Gender = ((DropDownList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("Gender")).SelectedValue;
		userProfile.Save();

		// Add role to current created user
		Roles.AddUserToRole(CreateUser.UserName, ((DropDownList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("UserRole")).SelectedValue);

		// Save profile of current created user
		SaveUserProfile();
	}

	// Save new user profile to custom table
	protected void SaveUserProfile()
	{
		MembershipUser newprofile = Membership.GetUser(CreateUser.UserName);

		// Get the UserId of the just-added user
		Guid UserId = (Guid)newprofile.ProviderUserKey;

		// Get firstname from CreateUserWizard control
		string FirstName = ((TextBox)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;

		// Get lastname from CreateUserWizard control
		string LastName = ((TextBox)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;

		// Get gender from CreateUserWizard control
		string Gender = ((DropDownList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("Gender")).SelectedValue;

		DbCommand cmd = GenericDataAccess.CreateCommand();
		cmd.CommandText = "EF_CreateUserProfile";

		DbParameter UserParam = cmd.CreateParameter();
		UserParam.ParameterName = "@UserID";
		UserParam.Value = UserId;
		cmd.Parameters.Add(UserParam);

		UserParam = cmd.CreateParameter();
		UserParam.ParameterName = "@FirstName";
		UserParam.Value = FirstName;
		cmd.Parameters.Add(UserParam);

		UserParam = cmd.CreateParameter();
		UserParam.ParameterName = "@LastName";
		UserParam.Value = LastName;
		cmd.Parameters.Add(UserParam);

		UserParam = cmd.CreateParameter();
		UserParam.ParameterName = "@Gender";
		UserParam.Value = Gender;
		cmd.Parameters.Add(UserParam);

		GenericDataAccess.ExecuteCommand(cmd);
	}
}